package com.shoppingmall.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shoppingmall.R;
import com.shoppingmall.bean.ShopCartBean;
import com.shoppingmall.bean.ShopCartGroupBean;

import java.util.List;
import java.util.Map;


public class ShopCartListAdapter extends BaseExpandableListAdapter {
    private List<ShopCartGroupBean> group;
    private Map<String, List<ShopCartBean>> children;
    private Context context;

    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;

    public ShopCartListAdapter(Context context, List<ShopCartGroupBean> group, Map<String, List<ShopCartBean>> children) {
        this.group = group;
        this.children = children;
        this.context = context;
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return children.get(group.get(groupPosition).getSeriesName()).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<ShopCartBean> childs = children.get(group.get(groupPosition).getSeriesName());
        return childs.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder gholder;
        if (convertView == null) {
            gholder = new GroupHolder();
            convertView = View.inflate(context, R.layout.item_shopcart_outer, null);
            gholder.cb_series = (CheckBox) convertView.findViewById(R.id.cb_series);
            convertView.setTag(gholder);
        } else {
            gholder = (GroupHolder) convertView.getTag();
        }
        final ShopCartGroupBean groupBean = group.get(groupPosition);
        gholder.cb_series.setText(groupBean.getSeriesName());
        gholder.cb_series.setChecked(groupBean.isChoosed());
        gholder.cb_series.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                groupBean.setChoosed(((CheckBox) v).isChecked());
                checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());//暴露组选接口
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildHolder cholder;
        if (convertView == null) {
            cholder = new ChildHolder();
            convertView = View.inflate(context, R.layout.item_shopcart, null);
            cholder.cb_check = (CheckBox) convertView.findViewById(R.id.check_box);
            cholder.iv_pic = (ImageView) convertView.findViewById(R.id.iv_pic);
            cholder.tv_product_name = (TextView) convertView.findViewById(R.id.tv_introduce);
            cholder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            cholder.tv_scale = (TextView) convertView.findViewById(R.id.tv_scale);
            cholder.iv_increase = (ImageView) convertView.findViewById(R.id.iv_add);
            cholder.iv_decrease = (ImageView) convertView.findViewById(R.id.iv_reduce);
            cholder.tv_count = (TextView) convertView.findViewById(R.id.tv_num);
            convertView.setTag(cholder);
        } else {
            cholder = (ChildHolder) convertView.getTag();
        }
        final ShopCartBean product = (ShopCartBean) getChild(groupPosition, childPosition);
        if (product != null) {
            cholder.tv_product_name.setText(product.getCommName());
            cholder.tv_scale.setText(product.getScale());
            cholder.tv_price.setText("￥" + product.getaPrice());
            cholder.tv_count.setText(product.getNum() + "");
//            GlideUtils.loadImageView(context, Constants.SERVER + product.getImg(), R.drawable.placeholder_image, cholder.iv_pic);
            cholder.cb_check.setChecked(product.isChoosed());
            cholder.cb_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    product.setChoosed(((CheckBox) v).isChecked());
                    cholder.cb_check.setChecked(((CheckBox) v).isChecked());
                    checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());//暴露子选接口
                }
            });
            cholder.iv_increase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doIncrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked());//暴露增加接口
                }
            });
            cholder.iv_decrease.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doDecrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked());//暴露删减接口
                }
            });
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    /**
     * 组元素绑定器
     *
     * @author chenjunsen
     * 2015年8月18日下午8:56:15
     */
    private class GroupHolder {
        CheckBox cb_series;
    }

    /**
     * 子元素绑定器
     *
     * @author chenjunsen
     * 2015年8月18日下午8:56:25
     */
    private class ChildHolder {
        CheckBox cb_check;
        ImageView iv_pic;
        TextView tv_product_name;
        TextView tv_price;
        TextView tv_scale;
        ImageView iv_increase;
        TextView tv_count;
        ImageView iv_decrease;
    }

    /**
     * 复选框接口
     *
     * @author chenjunsen
     * 2015年8月18日下午8:56:39
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param groupPosition 组元素位置
         * @param isChecked     组元素选中与否
         */
        void checkGroup(int groupPosition, boolean isChecked);

        /**
         * 子选框状态改变时触发的事件
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param isChecked     子元素选中与否
         */
        void checkChild(int groupPosition, int childPosition, boolean isChecked);
    }

    /**
     * 改变数量的接口
     *
     * @author chenjunsen
     * 2015年8月18日下午8:56:50
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);
    }

}
